// 面包屑导航
import {getStore,setStore} from './myUtil'
export function breadcrumb(router) {
    let routeList = getStore('routeList') || [];
    router.beforeEach((to,from,next)=>{
        // 数组去重
        if(routeList.length>0){
            let obj = {};
            routeList = routeList.reduce(function(item,next){
                obj[next.path] ? "" : (obj[next.path] = true && item.push(next));
                return item;
            },[])
        }

        let index = -1;
        for (let i = 0; i < routeList.length; i++) {
            if(routeList[i].name == to.meta.title){
                index = i;
                break;
            }
        }

        let nameArr = ["我的工作站",];
        for (let i in nameArr) {
            if (nameArr[i] == to.meta.title) {
                routeList = [];
                index = -1;
                break;
            }
        }

        if(index !== -1){
            routeList.splice(index+1,routeList.length-index-1);
        }else if(to.meta.title != "登录"){
            routeList.push({name:to.meta.title,path:to.fullPath});
        }
        to.meta.routeList = routeList;
        setStore("routeList",routeList);
        next();
    })
}